package com.mock.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.mock.dao.OrganisationDao;
import com.mock.model.DataTable;
import com.mock.model.Organisation;
import com.mock.utils.HibernateUtil;

public class OrganisationDaoImpl extends GenericDaoImpl<Organisation, Long>
		implements OrganisationDao {

	@Override
	public List<Organisation> getAllOrganisation() {
		return super.getAll(Organisation.class);
	}

	@Override
	public Organisation getById(long id) {
		return super.getById(Organisation.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Organisation> getOrganisationByQuery(DataTable datatable) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Organisation.class, "organisation");
		criteria.createAlias("organisation.leadContact", "contact");
		criteria.setFetchMode("organisation.leadContact", FetchMode.JOIN);			
		
		// Filter by Letter
		Disjunction disjunction = Restrictions.disjunction();
		if ("^[0-9]".equalsIgnoreCase(datatable.sSearch_0)) {
			for (int i = 0; i < 10; i++) {
				disjunction
						.add(Restrictions.ilike("organisationName", i + "%"));
			}
			criteria.add(disjunction);
		} else {
			criteria.add(Restrictions.ilike("organisationName",
					datatable.sSearch_0.replace("^", "") + "%"));
		}

		// Get Sort Column Name
		String columnName = "organisationName";
		switch (datatable.iSortCol_0) {
		case 0:
			columnName = "organisationName";
			break;
		case 1:
			columnName = "addressLine1";
			break;
		case 2:
			columnName = "postcode";
			break;
		case 3:
			if ("asc".equalsIgnoreCase(datatable.sSortDir_0)) {
				criteria.addOrder(Order.asc("contact.firstname"));
				criteria.addOrder(Order.asc("contact.surname"));
			} else {
				criteria.addOrder(Order.desc("contact.firstname"));
				criteria.addOrder(Order.desc("contact.surname"));
			}
			columnName = "contact.firstname";
			break;
		case 4:
			columnName = "active";
			break;
		default:
			break;
		}

		// Get Sort Direction
		if ("asc".equalsIgnoreCase(datatable.sSortDir_0) && datatable.iSortCol_0 != 3) {
			criteria.addOrder(Order.asc(columnName));
		} else {
			criteria.addOrder(Order.desc(columnName));
		}
		// Filter in-active
		if ("".equalsIgnoreCase(datatable.sSearch_4)) {

		} else if ("Yes".equalsIgnoreCase(datatable.sSearch_4)) {
			criteria.add(Restrictions.eq("active", true));
		}

		// Paging
		criteria.setFirstResult(datatable.iDisplayStart);
		criteria.setMaxResults(datatable.iDisplayLength);

		List<Organisation> organisations = criteria.list();
		session.getTransaction().commit();
		return organisations;
	}

	@Override
	public int getRowCount() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		int count = ((Long) session.createQuery(
				"select count(*) from Organisation").uniqueResult()).intValue();
		session.getTransaction().commit();
		return count;
	}
	
	@Override
	public Long getTotalRecordByQuery(DataTable datatable) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Organisation.class, "organisation");
		criteria.createAlias("organisation.leadContact", "contact");
		criteria.setFetchMode("organisation.leadContact", FetchMode.JOIN);			
		
		// Filter by Letter
		Disjunction disjunction = Restrictions.disjunction();
		if ("^[0-9]".equalsIgnoreCase(datatable.sSearch_0)) {
			for (int i = 0; i < 10; i++) {
				disjunction
						.add(Restrictions.ilike("organisationName", i + "%"));
			}
			criteria.add(disjunction);
		} else {
			criteria.add(Restrictions.ilike("organisationName",
					datatable.sSearch_0.replace("^", "") + "%"));
		}
		
		// Filter in-active
		if ("".equalsIgnoreCase(datatable.sSearch_4)) {

		} else if ("Yes".equalsIgnoreCase(datatable.sSearch_4)) {
			criteria.add(Restrictions.eq("active", true));
		}

		Long sizes = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();		
		session.getTransaction().commit();
		return sizes;
	}

}
